package com.example.luke.cashorderingsystem.Adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.luke.cashorderingsystem.Model.GoodType;
import com.example.luke.cashorderingsystem.R;
import com.example.luke.cashorderingsystem.Util.GlobalFunction;

import org.json.JSONObject;

import java.util.ArrayList;

/**
 * Created by luke on 15/11/2016.
 */

public class GoodTypeAdapter extends BaseAdapter {
    private  int pos=0;

   public interface Callback{

       void typeSelected(Integer pos);

    }

    //public  static String goodTypeListKey="goodTypeList";//本地数据的KEY
    private ArrayList<GoodType> goodTypeArrayList;

    private Context context;
    private Callback callback;

   private LayoutInflater  layoutInflater;

    public void setCallback(Callback callback) {
        this.callback = callback;
    }

    public GoodTypeAdapter(Context context){
       layoutInflater=LayoutInflater.from(context);
        goodTypeArrayList=new ArrayList<>();
        this.context=context;

    }

    public void setGoodTypeArrayList(ArrayList<GoodType> goodTypeArrayList) {
        this.goodTypeArrayList = goodTypeArrayList;
    }

    public void addJsonObject(JSONObject object){
        try {
            goodTypeArrayList.add(new GoodType(object.getString("id"),object.getString("name")));


        }catch (Exception e){

            GlobalFunction.printTextByTag(e.getMessage());
        }

    }

    @Override
    public int getCount() {
        return goodTypeArrayList.size();
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public GoodType getItem(int position) {
        return goodTypeArrayList.get(position);

    }

    public ArrayList<GoodType> getGoodTypeArrayList() {
        return goodTypeArrayList;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView=layoutInflater.inflate(R.layout.good_type_row,null);

        }
        convertView.setId(position);

        TextView goodName=(TextView)convertView.findViewById(R.id.goodName);
        goodName.setText(getItem(position).getName());
        goodName.setTag(String.valueOf(position));

//        if (position==pos){//默认第一个为选中
//            goodName.setSelected(true);
//        }

//        goodName.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                pos=Integer.valueOf((String)v.getTag());
//
//
//
//                if (callback!=null){
//                    callback.typeSelected(pos);
//                }
////                v.setSelected(true);
//
//            }
//        });

        return convertView;

    }

//    @Override
//    public void onClick(View v) {
//        post=v.getId();
//
//
//
//        notifyDataSetChanged();
//        GlobalFunction.printTextByTag(callback+"");
//        if (callback!=null){
//
//            callback.typeSelected(getItem(v.getId()).getId());
//        }
//
//
//    }
}
